package org.anivision.forms{

import flash.display.DisplayObjectContainer;
import flash.events.*;
import org.anivision.eval.Eval;
import org.aswing.*;
import org.aswing.border.*;
import org.aswing.geom.*;
import org.aswing.colorchooser.*;
import org.aswing.ext.*;

/**
 * PromptPanel
 * @private
 */
public class PromptPanel extends JPanel{
	
	//members define
	private var _top_panel:JPanel;
	private var _menubar:JMenuBar;
	private var _run:JMenu;
	private var _left_tools:JToolBar;
	private var _close:JButton;
	private var _prompt_scroll:JScrollPane;
	private var _prompt:JTextArea;
	private var _field:JTextField;
	private var _owner:DisplayObjectContainer;
	/**
	 * PromptPanel Constructor
	 */
	public function PromptPanel(owner:DisplayObjectContainer) {
		_owner = owner;
		//component creation
		setSize(new IntDimension(400, 400));
		var border0:LineBorder = new LineBorder();
		border0.setColor(new ASColor(0x0, 1));
		setBorder(border0);
		var layout1:BorderLayout = new BorderLayout();
		layout1.setVgap(1);
		setLayout(layout1);
		
		_top_panel = new JPanel();
		_top_panel.setSize(new IntDimension(400, 400));
		_top_panel.setConstraints("North");
		var border2:CaveBorder = new CaveBorder();
		border2.setBeveled(true);
		_top_panel.setBorder(border2);
		var layout3:BorderLayout = new BorderLayout();
		_top_panel.setLayout(layout3);
		
		_menubar = new JMenuBar();
		_menubar.setConstraints("Center");
		
		_run = new JMenu();
		_run.setText("Run");
		
		_left_tools = new JToolBar();
		_left_tools.setConstraints("East");
		_left_tools.setOrientation(0);
		
		_close = new JButton();
		_close.setFont(new ASFont("Tahoma", 10, true, false, false, false));
		_close.setText("X");
		
		_prompt_scroll = new JScrollPane();
		_prompt_scroll.setConstraints("Center");
		var border4:BevelBorder = new BevelBorder();
		border4.setBevelType(1);
		border4.setThickness(1);
		_prompt_scroll.setBorder(border4);
		
		_prompt = new JTextArea();
		_prompt.setOpaque(true);
		_prompt.setForeground(new ASColor(0x0, 1));
		_prompt.setBackground(new ASColor(0xcccccc, 1));
		_prompt.setConstraints("Center");
		var border5:LineBorder = new LineBorder();
		border5.setColor(new ASColor(0x0, 1));
		_prompt.setBorder(border5);
		_prompt.setEditable(false);
		_prompt.setWordWrap(true);
		
		_field = new JTextField();
		_field.setForeground(new ASColor(0x0, 1));
		_field.setBackground(new ASColor(0xffffff, 1));
		_field.setConstraints("South");
		var border6:BevelBorder = new BevelBorder();
		border6.setBevelType(1);
		border6.setThickness(1);
		_field.setBorder(border6);
		_field.setEditable(true);
		
		//component layoution
		append(_top_panel);
		append(_prompt_scroll);
		append(_field);
		
		_top_panel.append(_menubar);
		_top_panel.append(_left_tools);
		
		_menubar.append(_run);
		
		_left_tools.append(_close);
		
		_prompt_scroll.append(_prompt);
		_prompt.setWordWrap(false);
		
	}

	public function init():void {
		Eval.singleton.addEventListener(Event.CHANGE, __onEvalChange);
		_field.addEventListener(KeyboardEvent.KEY_UP, __onKeyUp);
	}
	
	private function __onKeyUp(e:KeyboardEvent): void {
		switch(e.keyCode)
		{
			case 13:
				__onFieldRun();
			break;
		}
	}
	
	private function __onFieldRun():void {
		try{
			Eval.singleton.eval(_field.getText());
		}
		catch (e) {
			trace(e);
		}
		finally{
			_field.setText('');
		}
		
	}
	private function __onEvalChange(e:Event): void {
		_prompt.setText(Eval.singleton.history);
		_prompt.scrollToBottomLeft();
	}
	
	//_________getters_________
	
	
	public function get_menubar():JMenuBar{
		return _menubar;
	}
	
	
	public function get_left_tools():JToolBar{
		return _left_tools;
	}
	
	
	
	
	
	
}
}
